package edu.tjpu.share.newui;

import java.io.File;
import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import edu.tjpu.share.R;
import edu.tjpu.share.app.Fields;
import edu.tjpu.share.interfaces.IPadCallback;
import edu.tjpu.share.util.BitmapUtil;
import edu.tjpu.share.util.ColorSelector;
import edu.tjpu.share.util.ColorSelector.ColorSelectorCallback;

public class PicturePad extends Activity implements View.OnClickListener,
		IPadCallback {

	private static final int PAD_PEN_COLOR = 0x01;
	private static final int PAD_BK_COLOR = 0x02;

	private PadView m_pad = null;
	private ColorSelector m_penColorSelector = null;
	private int m_colorSelectorType = PAD_PEN_COLOR;
	private Button m_chooseBtn = null;
	private Button m_penBtn = null;
	private Button m_eraserBtn = null;
	private Button m_clearBtn = null;
	private Button m_undoBtn = null;
	private Button m_redoBtn = null;
	private Button m_saveBtn = null;
	private Button m_loadBtn = null;
	private Button m_pencolorBtn = null;
	private Button m_bkcolorBtn = null;
	private Button m_pensizeBtn = null;
	private Bitmap bmp = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pad);
		m_pad = (PadView) this.findViewById(R.id.pad);
		m_penColorSelector = (ColorSelector) this
				.findViewById(R.id.pencolorselector);
		m_penColorSelector.setCallback(new ColorSelectorCallback() {

			@Override
			public void onColorSelectDone(ColorSelector sender) {
				// 完成颜色选择
				int color = sender.getPenColor();
				switch (m_colorSelectorType) {
				case PAD_PEN_COLOR:
					Toast.makeText(PicturePad.this, "选择比划颜色",
							Toast.LENGTH_SHORT).show();
					m_pad.setStrokeColor(color);
					break;

				case PAD_BK_COLOR:
					Toast.makeText(PicturePad.this, "选择背景颜色",
							Toast.LENGTH_SHORT).show();
					m_pad.setBkColor(color);
					break;
				}
			}

			@Override
			public void onColorSelectChange(ColorSelector sender) {
				// 改变颜色
				int color = sender.getPenColor();
				switch (m_colorSelectorType) {
				case PAD_PEN_COLOR:
					break;

				case PAD_BK_COLOR:
					m_pad.setBkColor(color);
					break;
				}
			}

			@Override
			public void onColorSelectCancel(ColorSelector sender) {
				// 取消颜色选择
				int color = sender.getPenColor();
				switch (m_colorSelectorType) {
				case PAD_PEN_COLOR:
					break;

				case PAD_BK_COLOR:
					m_pad.setBkColor(color);
					break;
				}
			}
		});

		m_penBtn = (Button) this.findViewById(R.id.pen);
		m_eraserBtn = (Button) this.findViewById(R.id.eraser);
		m_clearBtn = (Button) this.findViewById(R.id.clear);
		m_undoBtn = (Button) this.findViewById(R.id.undo);
		m_redoBtn = (Button) this.findViewById(R.id.redo);
		m_saveBtn = (Button) this.findViewById(R.id.save);
		m_loadBtn = (Button) this.findViewById(R.id.load);
		m_pencolorBtn = (Button) this.findViewById(R.id.penColor);
		m_bkcolorBtn = (Button) this.findViewById(R.id.bkColor);
		m_pensizeBtn = (Button) this.findViewById(R.id.penSize);
		m_chooseBtn = (Button) this.findViewById(R.id.choose);

		m_penBtn.setOnClickListener(this);
		m_eraserBtn.setOnClickListener(this);
		m_clearBtn.setOnClickListener(this);
		m_undoBtn.setOnClickListener(this);
		m_redoBtn.setOnClickListener(this);
		m_saveBtn.setOnClickListener(this);
		m_loadBtn.setOnClickListener(this);
		m_pencolorBtn.setOnClickListener(this);
		m_bkcolorBtn.setOnClickListener(this);
		m_pensizeBtn.setOnClickListener(this);
		m_chooseBtn.setOnClickListener(this);

		m_pad.setCallback(this);

		m_penBtn.setEnabled(false);
		m_clearBtn.setEnabled(false);
		m_redoBtn.setEnabled(false);
		m_undoBtn.setEnabled(false);

		Bundle bundle = getIntent().getExtras();
		bmp = (Bitmap) bundle.getParcelable(Fields.BITMAP_TO_DO);
		Log.d("debug", "getMap");
		onLoadClick();
	}



	@Override
	public void onTouchDown(PadView obj, MotionEvent event) {
		m_clearBtn.setEnabled(true);
		m_undoBtn.setEnabled(true);
	}

	@Override
	public void onTouchUp(PadView obj, MotionEvent event) {

	}

	@Override
	public void onDestroy(PadView obj) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pen:
			onPenClick(v);
			break;

		case R.id.eraser:
			onEraseClick(v);
			break;

		case R.id.clear:
			onClearClick(v);
			break;

		case R.id.undo:
			onUndoClick(v);
			break;

		case R.id.redo:
			onRedoClick(v);
			break;

		case R.id.save:
			onSaveClick(v);
			break;

		case R.id.load:
			// onLoadClick(v);
			break;

		case R.id.penColor:
			onPenColorClick(v);
			break;

		case R.id.bkColor:
			onBkColorClick(v);
			break;

		case R.id.penSize:
			onPenSizeClick(v);
			break;
		default:
			break;
		}
	}

	protected void onPenClick(View v) {
		Toast.makeText(this, "画笔", Toast.LENGTH_SHORT).show();
		m_pad.setStrokeType(Fields.STROKE_PEN);
		m_penBtn.setEnabled(false);
		m_eraserBtn.setEnabled(true);
	}

	protected void onEraseClick(View v) {
		Toast.makeText(this, "橡皮擦", Toast.LENGTH_SHORT).show();
		m_pad.setStrokeType(Fields.STROKE_ERASER);
		m_penBtn.setEnabled(true);
		m_eraserBtn.setEnabled(false);
	}

	protected void onClearClick(View v) {
		Toast.makeText(this, "清除", Toast.LENGTH_SHORT).show();
		m_pad.clearAllStrokes();
		m_clearBtn.setEnabled(false);
		m_redoBtn.setEnabled(false);
		m_undoBtn.setEnabled(false);
	}

	protected void onUndoClick(View v) {
		Toast.makeText(this, "撤销", Toast.LENGTH_SHORT).show();
		m_pad.undo();
		m_undoBtn.setEnabled(m_pad.canUndo());
		m_redoBtn.setEnabled(m_pad.canRedo());
	}

	protected void onRedoClick(View v) {
		Toast.makeText(this, "重做", Toast.LENGTH_SHORT).show();
		m_pad.redo();
		m_undoBtn.setEnabled(m_pad.canUndo());
		m_redoBtn.setEnabled(m_pad.canRedo());
	}

	protected void onSaveClick(View v) {
		Toast.makeText(this, "保存", Toast.LENGTH_SHORT).show();
		String strFilePath = getStrokeFilePath();
		Bitmap bmp = m_pad.getCanvasSnapshot();
		if (null != bmp) {
			//TODO 传bitmap
			BitmapUtil.saveBitmapToSDCard(bmp, strFilePath);
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			
			//bundle.putSerializable(Fields.BITMAP_DONE, bmp);
//			bundle.putParcelable(Fields.BITMAP_DONE, bmp);
			bundle.putString(Fields.BITMAP_PATH, strFilePath);
			intent.putExtras(bundle);
			//intent.setClass(PicturePad.this, NewMessage.class);
			setResult(RESULT_OK,intent);
			//startActivity(intent);
			finish();
		}
	}
/*
	protected void onLoadClick(View v) {
		if (null != bmp) {
			m_pad.setForeBitmap(bmp);
		}

		m_clearBtn.setEnabled(true);
	}*/

	protected void onLoadClick() {
		if (null != bmp) {
			m_pad.setBkBitmap(bmp);
		}

		m_clearBtn.setEnabled(true);
	}

	protected void onPenColorClick(View v) {
		m_colorSelectorType = PAD_PEN_COLOR;
		if (null != m_penColorSelector) {
			m_penColorSelector.setVisibility(View.VISIBLE);
		}
	}

	protected void onBkColorClick(View v) {
		m_colorSelectorType = PAD_BK_COLOR;
		if (null != m_penColorSelector) {
			m_penColorSelector.setVisibility(View.VISIBLE);
		}
	}

	protected void onPenSizeClick(View v) {
	}

	/*
	 * protected void onChoosePictureClick(View v) { Intent intent = new
	 * Intent(this, PictureSelectActivity.class); startActivityForResult(intent,
	 * 0); }
	 */

	public String getStrokeFilePath() {
		File sdcarddir = android.os.Environment.getExternalStorageDirectory();
		String strDir = sdcarddir.getPath() + "/share/data/pad/";
		String strFileName = getStrokeFileName();
		File file = new File(strDir);
		if (!file.exists()) {
			file.mkdirs();
		}

		String strFilePath = strDir + strFileName;

		return strFilePath;
	}

	public String getStrokeFileName() {
		String strFileName = "";

		Calendar rightNow = Calendar.getInstance();
		int year = rightNow.get(Calendar.YEAR);
		int month = rightNow.get(Calendar.MONDAY);
		int date = rightNow.get(Calendar.DATE);
		int hour = rightNow.get(Calendar.HOUR);
		int minute = rightNow.get(Calendar.MINUTE);
		int second = rightNow.get(Calendar.SECOND);

		strFileName = String.format("%02d%02d%02d%02d%02d%02d.png", year,
				month, date, hour, minute, second);
		return strFileName;
	}

}
